package com.canaan.business.service.storehouse.impl;

import java.util.Date;
import java.util.List;

import com.canaan.business.common.enums.inOutStock.InOutStockTypeEnum;
import com.canaan.business.common.enums.storehouse.InOutStorageOperationEnum;
import com.canaan.business.domain.basic.WmsStorehouseLocation;
import com.canaan.business.domain.storehouse.WmsInOutStorage;
import com.canaan.business.domain.storehouse.WmsStorehouseStock;
import com.canaan.business.service.basic.IWmsProductService;
import com.canaan.business.service.basic.IWmsStorehouseLocationService;
import com.canaan.business.service.storehouse.IWmsInOutStorageService;
import com.canaan.business.service.storehouse.IWmsStorehouseStockService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.TableNoUtil;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.storehouse.StockTransferMapper;
import com.canaan.business.domain.storehouse.StockTransfer;
import com.canaan.business.service.storehouse.IStockTransferService;

/**
 * 移库Service业务层处理
 *
 * @author 
 * @date 2023-02-17
 */
@Service
public class StockTransferServiceImpl implements IStockTransferService {
    @Resource
    private StockTransferMapper stockTransferMapper;
    @Autowired
    private IWmsStorehouseStockService storehouseStockService;
    @Autowired
    private IWmsStorehouseLocationService storehouseLocationService;
    @Autowired
    private IWmsInOutStorageService wmsInOutStorageService;
    @Autowired
    private IWmsProductService wmsProductService;
    /**
     * 查询移库
     *
     * @param id 移库主键
     * @return 移库
     */
    @Override
    public StockTransfer selectStockTransferById(Long id) {
        return stockTransferMapper.selectStockTransferById(id);
    }

    /**
     * 查询移库列表
     *
     * @param stockTransfer 移库
     * @return 移库
     */
    @Override
    public List<StockTransfer> selectStockTransferList(StockTransfer stockTransfer) {
        return stockTransferMapper.selectStockTransferList(stockTransfer);
    }

    /**
     * 新增移库
     *
     * @param stockTransfer 移库
     * @return 结果
     */
    @Override
    public int insertStockTransfer(StockTransfer stockTransfer) {
        if(StringUtils.isBlank(stockTransfer.getNo())){
            stockTransfer.setNo(TableNoUtil.getNo(StockTransfer.class));
        }
        if (StringUtils.isEmpty(stockTransfer.getBarcode())){
            throw new ServiceException("条码不能为空");
        }
        if (StringUtils.isEmpty(stockTransfer.getSourceLocationNo())){
            throw new ServiceException("来源库位不能为空");
        }
        if (StringUtils.isEmpty(stockTransfer.getSourceStorehouse())){
            throw new ServiceException("来源仓库不能为空");
        }
        if (StringUtils.isEmpty(stockTransfer.getTargetLocationNo())){
            throw new ServiceException("目标库位不能为空");
        }
        if (StringUtils.isEmpty(stockTransfer.getTargetStorehouse())){
            throw new ServiceException("目标仓库不能为空");
        }
        if(!stockTransfer.getSourceStorehouse().equalsIgnoreCase(stockTransfer.getTargetStorehouse())){
            throw new ServiceException("来源仓库与目标仓库必须一致");
        }
        WmsStorehouseStock storehouseStock =  storehouseStockService.getInfoByBarcode(stockTransfer.getBarcode());
        if (storehouseStock==null){
            throw new ServiceException("该条码的库存信息不存在");
        }
        if (stockTransfer.getSourceStorehouse().equals(StringUtils.isEmpty(stockTransfer.getTargetStorehouse()))){
            throw new ServiceException("来源仓必须等于目标仓");
        }
        WmsStorehouseLocation storehouseSoureLocation = storehouseLocationService.selectWmsStorehouseLocationByNo(stockTransfer.getSourceLocationNo());
        if (storehouseSoureLocation==null){
            throw new ServiceException("来源库位不存在");
        }
        WmsStorehouseLocation storehouseLocation = storehouseLocationService.selectWmsStorehouseLocationByNo(stockTransfer.getTargetLocationNo());
        if (storehouseLocation==null){
            throw new ServiceException("目标库位不存在");
        }
        //验证目标库位是否可用
        storehouseStockService.getInfoByLocationNo(stockTransfer.getTargetLocationNo());
        storehouseStock.setStorehouseLocationNo(stockTransfer.getTargetLocationNo());
        storehouseStock.setStorehouseAreaNo(storehouseLocation.getStorehouseAreaNo());
        storehouseStockService.updateWmsStorehouseStock(storehouseStock);
        stockTransfer.setCreateBy(SecurityUtils.getUsername());
        stockTransfer.setCreateTime(DateUtils.getNowDate());
        stockTransfer.setRemark("库内转储");
        //生成出库记录
        WmsInOutStorage outStorageSj = new WmsInOutStorage();
        outStorageSj.setType(InOutStockTypeEnum.MOVE.getCode().longValue());
        outStorageSj.setNo(TableNoUtil.getNo(WmsInOutStorage.class));
        outStorageSj.setProductNo(stockTransfer.getProductNo());
        outStorageSj.setProductName(storehouseStock.getProductName());
        outStorageSj.setProductUnit(storehouseStock.getCompany());
        outStorageSj.setStorehouseNo(stockTransfer.getSourceStorehouse());
        outStorageSj.setAreaNo(storehouseSoureLocation.getStorehouseAreaNo());
        outStorageSj.setStorehouseLocationNo(stockTransfer.getSourceLocationNo());
        outStorageSj.setBatchNo(storehouseStock.getBatchNo());
        outStorageSj.setBarcode(storehouseStock.getBarcode());
        outStorageSj.setQuantity(storehouseStock.getQty());
        outStorageSj.setRelNo(stockTransfer.getNo());
        outStorageSj.setOperation(InOutStorageOperationEnum.OUT.getCode());
        outStorageSj.setCreateBy(SecurityUtils.getUsername());
        outStorageSj.setCreateTime(new Date());
        wmsInOutStorageService.addWmsInOutStorage(outStorageSj);
        //生成入库记录
        WmsInOutStorage inStorageSj = new WmsInOutStorage();
        inStorageSj.setType(InOutStockTypeEnum.MOVE.getCode().longValue());
        inStorageSj.setNo(TableNoUtil.getNo(WmsInOutStorage.class));
        inStorageSj.setProductNo(stockTransfer.getProductNo());
        inStorageSj.setProductName(storehouseStock.getProductName());
        inStorageSj.setProductUnit(storehouseStock.getCompany());
        inStorageSj.setStorehouseNo(stockTransfer.getTargetStorehouse());
        inStorageSj.setAreaNo(storehouseLocation.getStorehouseAreaNo());
        inStorageSj.setStorehouseLocationNo(stockTransfer.getTargetLocationNo());
        inStorageSj.setBatchNo(storehouseStock.getBatchNo());
        inStorageSj.setBarcode(storehouseStock.getBarcode());
        inStorageSj.setQuantity(storehouseStock.getQty());
        inStorageSj.setRelNo(stockTransfer.getNo());
        inStorageSj.setOperation(InOutStorageOperationEnum.IN.getCode());
        inStorageSj.setCreateBy(SecurityUtils.getUsername());
        inStorageSj.setCreateTime(new Date());
        wmsInOutStorageService.addWmsInOutStorage(inStorageSj);
        return stockTransferMapper.insertStockTransfer(stockTransfer);
    }

    /**
     * 批量新增移库
     *
     * @param stockTransferList 移库列表
     * @return 结果
     */
    @Override
    public int batchInsertStockTransfer(List<StockTransfer> stockTransferList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockTransfer stockTransfer : stockTransferList){
            if(StringUtils.isBlank(stockTransfer.getNo())){
                stockTransfer.setNo(TableNoUtil.getNo(StockTransfer.class));
            }
            stockTransfer.setCreateBy(username);
            stockTransfer.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<stockTransferList.size();) {
            int endIndex = i+500;
            if (endIndex > stockTransferList.size()) {
                endIndex = stockTransferList.size();
            }
            insertNum = insertNum + stockTransferMapper.batchInsertStockTransfer(stockTransferList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改移库
     *
     * @param stockTransfer 移库
     * @return 结果
     */
    @Override
    public int updateStockTransfer(StockTransfer stockTransfer) {
        stockTransfer.setUpdateBy(SecurityUtils.getUsername());
        stockTransfer.setUpdateTime(DateUtils.getNowDate());
        return stockTransferMapper.updateStockTransfer(stockTransfer);
    }

    /**
     * 批量修改移库
     *
     * @param stockTransferList 移库列表
     * @return 结果
     */
    @Override
    public int batchUpdateStockTransfer(List<StockTransfer> stockTransferList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (StockTransfer stockTransfer : stockTransferList){
            stockTransfer.setUpdateBy(username);
            stockTransfer.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<stockTransferList.size();) {
            int endIndex = i+500;
            if (endIndex > stockTransferList.size()) {
                endIndex = stockTransferList.size();
            }
            updateNum = updateNum + stockTransferMapper.batchUpdateStockTransfer(stockTransferList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除移库
     *
     * @param ids 需要删除的移库主键集合
     * @return 结果
     */
    @Override
    public int deleteStockTransferByIds(Long[] ids) {
        return stockTransferMapper.deleteStockTransferByIds(ids);
    }

    /**
     * 删除移库信息
     *
     * @param id 移库主键
     * @return 结果
     */
    @Override
    public int deleteStockTransferById(Long id) {
        return stockTransferMapper.deleteStockTransferById(id);
    }

    /**
     * 查询移库
     *
     * @param no 编号
     * @return 移库
     */
    @Override
    public StockTransfer selectStockTransferByNo(String no) {
        return stockTransferMapper.selectStockTransferByNo(no);
    }

    /**
     * 批量删除移库
     *
     * @param nos 需要删除的编号集合
     * @return 结果
     */
    @Override
    public int deleteStockTransferByNos(String[] nos) {
        return stockTransferMapper.deleteStockTransferByNos(nos);
    }

    /**
     * 删除移库信息
     *
     * @param no 编号
     * @return 结果
     */
    @Override
    public int deleteStockTransferByNo(String no) {
        return stockTransferMapper.deleteStockTransferByNo(no);
    }
}
